Page({

    /**
     * 页面的初始数据
     */
    data: {
        multiIndex:[0,0,0,0],
        multiIndexs:[0,0,0,0],
        KmultiIndex:[0,0,0,0],
        KmultiIndexs:[0,0,0,0],
        deliveryId:"",
        tapTakeOrder:false,
        hasSubmitOrder:false,
        hasSubmitOrders:false,
        index:0,
        indexs:0,
        deliveryTargetsite:0,
        taskTargetsite:0,
        site:['海乐','海晴','海欢','海蓝','海湛','海天','海沁','海花','海草','海风','海怡','海虹','海月',],
        money:[0,1,2,3,4,5,6,7,8,9,10],
        multiArray:[['2021年','2022年'],['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月'],['01日','02日','03日','04日','05日','06日','07日','08日','09日','10日','11日','12日','13日','14日','15日','16日','17日','18日','19日','20日','21日','22日','23日','24日','25日','26日','27日','28日','29日','30日','31日'],['09时','10时','11时','12时','13时','14时','15时','16时','17时','18时','19时','20时','21时','22时','23时',]],
        multiArrayS:[['2021-','2022-'],['1-','2-','3-','4-','-','6-','7-','8-','9-','10-','11-','12-'],['1-','2-','3-','4-','5-','6-','7-','8-','9-','10-','11-','12-','13-','14-','15-','16-','17-','18-','19-','20-','21-','22-','23-','24-','25-','26-','27-','28-','29-','30-','31-'],['9-','10-','11-','12-','13-','14-','15-','16-','17-','18-','19-','20-','21-','22-','23-',]],
        UserSignature:"",
        showTopTips: false,
        formData: {

        },
        rules: [

        {
            name: 'name',
            rules: {required: true, message: '姓名必填'},
        },  
        {
            name: 'mobile',
            rules: [{required: true, message: '电话必填'}, {mobile: true, message: '电话格式不对'}],
        }],

        kuaididian: ["菜鸟裹裹", "中通快递", "圆通快递","韵达快递","京东快递","顺丰快递","邮政","申通快递"],
        kuaidiIndex: 0,

        inputValue:"",

        Title:[
            {
                id:0,
                name:"快递代拿",
                isActive:true
            },
            {
                id:1,
                name:"跑腿服务",
                isActive:false
            },
    
        ]

    },

    //检测赏金变化并赋值
    bindPickerChange: function(e) {
        this.setData({
          index: e.detail.value
        })
      },

      bindPickerChanges: function(e) {
        this.setData({
          indexs: e.detail.value
        })
      },

      //检测送达地点变化并赋值
      deliverySiteChange: function(e) {
        this.setData({
          deliveryTargetsite: e.detail.value
        })
      },

      taskSiteChange: function(e) {
        this.setData({
          taskTargetsite: e.detail.value
        })
      },

    //检测文本框内容变化并赋值
    bindKeyInput: function (e) {
        this.setData({
          inputValue: e.detail.value
        })
      },

    // methods: {
        //检测表单内数据变化并赋值
        formInputChange(e) {
            const {field} = e.currentTarget.dataset
            this.setData({
                [`formData.${field}`]: e.detail.value,
            })
        },

        formInputChanges(e) {
          this.setData({
              deliveryId: e.detail.value,
          })
      },

        //检测快递单号数据变化并赋值
        bindkuaididianChange: function(e) {
            this.setData({
                kuaidiIndex: e.detail.value
            })
        },

        //跳转至快递接单大厅
        goToDelivery(){
          wx.navigateTo({
            url: '/pages/delivery/delivery'
          })
      },


      //跳转至跑腿接单大厅
        goToTask(){
          wx.navigateTo({
            url: '/pages/task/task'
         })
      },
        

      //跳转至我的订单
      goToMyOrder(){
        wx.navigateTo({
          url: '/pages/myorders/myorders'
       })
    },
        
        // bindChange(e) {
        //     const val = e.detail.value
        //     this.setData({
        //     year: this.data.years[val[0]],
        //     month: this.data.months[val[1]],
        //     day: this.data.days[val[2]],
        //     hour: this.data.hours[val[3]]
        //     })
        // },


        //跑腿期待时间列表值变化则改变值
        bindMultiPickerChange: function (e) {
            this.setData({
              multiIndex: e.detail.value
            })
          },
          //跑腿订单失效时间改变则改变值
          bindMultiPickerChanges: function (e) {
            this.setData({
              multiIndexs: e.detail.value
            })
          },
 
          //快递期待时间列表值变化则改变值
          KbindMultiPickerChange: function (e) {
            this.setData({
              KmultiIndex: e.detail.value
            })
          },
           //快递订单失效时间改变则改变值
          KbindMultiPickerChanges: function (e) {
            this.setData({
              KmultiIndexs: e.detail.value
            })
          },


          //快递接单提交函数
        submitForms() {
          var that = this
            that.selectComponent('#form').validate((valid, errors) => {
                if (!valid) {
                    const firstError = Object.keys(errors)
                    if (firstError.length) {
                        that.setData({
                            error: errors[firstError[0]].message
                        })
    
                    }
                } 
                else {
                    wx.showToast({
                        title: '校验通过'
                    })

                    console.log(that.data.multiArray[0][that.data.KmultiIndex[0]]+that.data.multiArray[1][that.data.KmultiIndex[1]]+that.data.multiArray[2][that.data.KmultiIndex[2]]+that.data.multiArray[3][that.data.KmultiIndex[3]])
                    console.log(that.data.multiArray[0][that.data.KmultiIndexs[0]]+that.data.multiArray[1][that.data.KmultiIndexs[1]]+that.data.multiArray[2][that.data.KmultiIndexs[2]]+that.data.multiArray[3][that.data.KmultiIndexs[3]])
                    console.log(that.data.site[that.data.deliveryTargetsite])
                    console.log(that.data.kuaididian[that.data.kuaidiIndex])
                    console.log(that.data.deliveryId)
                    console.log(that.data.index)
                    console.log(that.data.formData.mobile)

                          //上传用户信息              


                          wx.request({
                            url: 'http://119.23.252.15:10002/delivery/publish',
                            method:"POST",
                            data:{
                                "token": wx.getStorageSync('token'),
                                "type": "delivery",
                                "order": {

                                  "deliveryStation": that.data.kuaididian[that.data.kuaidiIndex],
                                  "packageKey": that.data.deliveryId,
                                  "reward": that.data.index,
                                  "consumerTel": that.data.formData.mobile,
                                  "expectedDate":that.data.multiArray[0][that.data.KmultiIndex[0]]+that.data.multiArray[1][that.data.KmultiIndex[1]]+that.data.multiArray[2][that.data.KmultiIndex[2]]+that.data.multiArray[3][that.data.KmultiIndex[3]],
                                  "cancelDate": that.data.multiArray[0][that.data.KmultiIndexs[0]]+that.data.multiArray[1][that.data.KmultiIndexs[1]]+that.data.multiArray[2][that.data.KmultiIndexs[2]]+that.data.multiArray[3][that.data.KmultiIndexs[3]],
                                  "targetSite": that.data.site[that.data.deliveryTargetsite]

                                }
                            }
                          })

                          that.setData({
                            hasSubmitOrder:true
                          })

                }
            })
        },

        //跑腿接单提交函数
        submitForm() {
            var that = this
            that.selectComponent('#form').validate((valid, errors) => {
                if (!valid) {
                    const firstError = Object.keys(errors)
                    if (firstError.length) {
                      that.setData({
                            error: errors[firstError[0]].message
                        })
    
                    }
                } 
                
                else {
                    wx.showToast({
                        title: '校验通过'
                    })

                    console.log(that.data.inputValue)
                    console.log(that.data.indexs)
                    console.log(that.data.formData.mobile)
                    console.log(that.data.site[that.data.taskTargetsite])
                    console.log(that.data.multiArray[0][that.data.multiIndex[0]]+that.data.multiArray[1][that.data.multiIndex[1]]+that.data.multiArray[2][that.data.multiIndex[2]]+that.data.multiArray[3][that.data.multiIndex[3]])
                    console.log(that.data.multiArray[0][that.data.multiIndexs[0]]+that.data.multiArray[1][that.data.multiIndexs[1]]+that.data.multiArray[2][that.data.multiIndexs[2]]+that.data.multiArray[3][that.data.multiIndexs[3]])

                    wx.request({
                      url: 'http://119.23.252.15:10002/task/publish',
                      method:"POST",
                      data:{
                        "token": wx.getStorageSync('token'),
                        "type": "task",
                        "order": {
                          "task": that.data.inputValue,
                          "reward": that.data.indexs,
                          "consumerTel": that.data.formData.mobile,
                          "targetSite": that.data.site[that.data.taskTargetsite],
                          "expectedDate": that.data.multiArray[0][that.data.multiIndex[0]]+that.data.multiArray[1][that.data.multiIndex[1]]+that.data.multiArray[2][that.data.multiIndex[2]]+that.data.multiArray[3][that.data.multiIndex[3]],
                          "cancelDate": that.data.multiArray[0][that.data.multiIndexs[0]]+that.data.multiArray[1][that.data.multiIndexs[1]]+that.data.multiArray[2][that.data.multiIndexs[2]]+that.data.multiArray[3][that.data.multiIndexs[3]]
                        }                  
                      }
                    })

                    that.setData({
                      hasSubmitOrders:true
                    })

                  }
            })
        },




    // },

    //点击标题改变样式
    handItemchange(e){
        const {index}=e.currentTarget.dataset;
        let {Title}=this.data;
        Title.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
        this.setData({
          kuaidiIndex:0,
          index:0,
          deliveryTargetsite:0,
          KmultiIndex:[0,0,0,0],
          KmultiIndexs:[0,0,0,0],
          inputValue:"",
          indexs:0,
          taskTargetsite:0,
          multiIndex:[0,0,0,0],
          multiIndexs:[0,0,0,0],
          hasSubmitOrder:false,
          hasSubmitOrders:false,
            Title
        })
    },

    onLoad(){
      this.setData({
        kuaidiIndex:0,
        index:0,
        deliveryTargetsite:0,
        KmultiIndex:[0,0,0,0],
        KmultiIndexs:[0,0,0,0],
        inputValue:"",
        indexs:0,
        taskTargetsite:0,
        multiIndex:[0,0,0,0],
        multiIndexs:[0,0,0,0],
        hasSubmitOrder:false,
        hasSubmitOrders:false
      })
    },


    onReady(){
        var that = this
        wx.getStorage({ 
          key: 'UserSignature',
          success(res){
              that.setData({
                UserSignature:res.data
              })
          }
        })
    }
})